package com.example.chatweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.chatweb.entity.Article;
import com.example.chatweb.request.*;
import com.example.chatweb.service.ReportService;
import com.example.chatweb.utils.HttpUtil;
import com.example.chatweb.utils.R;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
// 接收HTTP请求，调用ReportService处理请求

@RestController
public class ReportController {
    private final ReportService reportService;

    @Autowired
    private HttpUtil httpUtil;
    @Autowired
    public ReportController(ReportService reportService) {
        this.reportService = reportService;
    }
    @PostMapping("/findArticleByKeyword")
    // 处理GET方法请求体
    List<Article> findArticleByKeyword(@RequestBody FindArticleByKeywordRequest findArticleByKeywordRequest){
        try{
            System.out.println("这是: "+findArticleByKeywordRequest.getPageNo());
            return reportService.findArticleByKeyword(findArticleByKeywordRequest.getKeyWord(), findArticleByKeywordRequest.getPageNo(), findArticleByKeywordRequest.getPageSize());
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("/generateReport")
    public String generateReport(@RequestBody ReportRequest reportRequest) {
        // 处理 reportRequest 对象
        return reportService.generateReport(reportRequest);
    }

    @PostMapping("/extractOpinion")
    public String extractOpinion(@RequestBody OpinionRequest opinionRequest) {
        // 处理 opinionRequest 对象
        return reportService.extractOpinion(opinionRequest);
    }
    @PostMapping("/api/generate-report")
    public String uploadToDB(@RequestBody SavedReport savedReport){
        //System.out.println(reportService.uploadToDB(savedReport));
        return reportService.uploadToDB(savedReport);
    }
    @PostMapping("/api/get-report-content")
    public String getReportContent(@RequestBody JsonNode requestBody){
        String reportId = requestBody.get("report_id").asText();
        return reportService.getReportContentByReportId(reportId);
    }

    @PostMapping("/api/get-every-reportid")
    public String getEveryReportIDByUserid(@RequestBody JsonNode requestBody){
        String userid = requestBody.get("user_id").asText();
        return reportService.getEveryReportIDByUserid(userid);
    }

    @PostMapping("/extractTestTXT")
    public R extractTest(@RequestBody ExtractTxtRequest extractTxtRequest) {
        System.out.println(extractTxtRequest);
        try {
            String url = "http://172.21.50.190:10302/extract_text";
            JSONObject post = httpUtil.post(url,extractTxtRequest);
//            错误控制，检测code，0是错误，1是正确，msg就会返回错误信息
            return R.ok(post.getString("data"));
        } catch (Exception e) {
            e.printStackTrace();
            return R.failure("输出错误信息" + e.getMessage());
        }

    }
    @PostMapping("/ExtractTestPdf")
    public R extractTestPdf(@RequestParam("file") MultipartFile file) {
        if (file == null) {
            return R.failure("传入的文件为空");
        }
        try{
            String url = "http://172.21.50.190:10302/extract_pdf";
            JSONObject post = httpUtil.postFileGetJSON(url, file);
            System.out.println("Pdf文件");
            System.out.println(post.getJSONArray("data"));
            return R.ok(post.getJSONArray("data"));
        }catch (Exception e) {
            e.printStackTrace();
            return R.failure("输出错误信息" + e.getMessage());
        }
    }
}
